package com.enjoy.xbase.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.enjoy.xbase.R;


/**
 * 对话框基础类
 * Created by cxb on 2017/3/10.
 */
public final class DialogUI {
    /***
     *
     * 描述：普通提示性对话框
     * @param context
     * @param hint
     */
    public static void createHint(Activity context, String hint) {
        createHintOne(context, hint, "确定", null);
    }

    /***
     *
     * 描述：普通提示性对话框(有返回)
     * @param context
     * @param hint
     */
    public static void createHint(Activity context, String title, String hint, String btn1, String btn2, final OnClickBack back) {
        Dialog dialog = new Builder(context)
                .setTitle(title, Gravity.CENTER)
                .setContent(hint)
                .setNegativeButton(btn1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        if (back != null) {
                            back.onLeft(dialog);
                        }
                    }
                })
                .setPositiveButton(btn2, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        if (back != null) {
                            back.onRight(dialog);
                        }
                        dialog.dismiss();
                    }
                })
                .create();
        if (context.isFinishing()) return;
        dialog.show();
    }

    /***
     *
     * 描述：普通提示性对话框(有返回)
     * @param context
     * @param hint
     */
    public static void createHint(Activity context, String hint, String btnText, final OnClickBack back) {
        Dialog dialog = new Builder(context)
                .setTitle("温馨提示", Gravity.CENTER)
                .setContent(hint)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        if (back != null) {
                            back.onLeft(dialog);
                        }
                    }
                })
                .setPositiveButton(btnText, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        if (back != null) {
                            back.onRight(dialog);
                        }
                        dialog.dismiss();
                    }
                })
                .create();
        if (context.isFinishing()) return;
        dialog.show();
    }

    /***
     *
     * 描述：普通提示性对话框(有返回)
     * @param context
     * @param hint
     */
    public static void createHintOne(Activity context, String hint, String btnText, final OnClickBack back) {
        Dialog dialog = new Builder(context)
                .setTitle("温馨提示")
                .setContent(hint)
                .setPositiveButton(btnText, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        if (back != null) {
                            back.onRight(dialog);
                        }
                        dialog.dismiss();
                    }
                })
                .create();
        if (context.isFinishing()) return;
        dialog.show();
    }

    public interface OnClickBack {
        void onLeft(DialogInterface dialog);

        void onRight(DialogInterface dialog);
    }

    public static class Builder {
        private Context context;

        private int themeResId = R.style.lb_dialog_custom;
        //top
        private boolean isShowTitle = true;
        private String title;
        private int gravity = Gravity.CENTER;
        private boolean close = false;
        //content
        private View contentView;
        private String strContent;
        //bottom
        private String negativeText, positiveText;
        private int negativeColor = Color.parseColor("#999999"), positiveColor = Color.parseColor("#03ce97");
        private DialogInterface.OnClickListener positiveListener, negativeListener;

        private Dialog dialog;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder(Context context, int themeResId) {
            this.context = context;
            this.themeResId = themeResId;
        }

        public Builder setTitle(String title, int gravity) {
            this.title = title;
            this.gravity = gravity;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setClose(boolean close) {
            this.close = close;
            return this;
        }

        public Builder setShowTitle(boolean isShow) {
            this.isShowTitle = isShow;
            return this;
        }

        public Builder setContent(View contentView) {
            this.contentView = contentView;
            return this;
        }

        public Builder setContent(String content) {
            this.strContent = content;
            return this;
        }

        /**
         * 右确定
         *
         * @param positiveText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(String positiveText, DialogInterface.OnClickListener listener) {
            this.positiveText = positiveText;
            this.positiveListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveText, int color, DialogInterface.OnClickListener listener) {
            this.positiveText = positiveText;
            this.positiveColor = color;
            this.positiveListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeText, DialogInterface.OnClickListener listener) {
            this.negativeText = negativeText;
            this.negativeListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeText, int color, DialogInterface.OnClickListener listener) {
            this.negativeText = negativeText;
            this.negativeColor = color;
            this.negativeListener = listener;
            return this;
        }

        public Dialog create() {
            dialog = new Dialog(context, themeResId);
            View baseView = LayoutInflater.from(context).inflate(R.layout.xbase_dialog_layout_custom, null);
            TextView tvTitle = baseView.findViewById(R.id.tv_title);
            ImageView ivClose = baseView.findViewById(R.id.iv_btn_close);
            TextView tvContent = baseView.findViewById(R.id.c_message);
            TextView tvPositive = baseView.findViewById(R.id.c_positiveButton);
            TextView tvNegative = baseView.findViewById(R.id.c_negativeButton);
            LinearLayout contView = baseView.findViewById(R.id.c_content);
            if (isShowTitle) {
                if (TextUtils.isEmpty(title)) {     //标题
                    tvTitle.setText("提示");
                } else {
                    tvTitle.setText(title);
                    tvTitle.setGravity(gravity);
                }
                ivClose.setVisibility(close ? View.VISIBLE : View.GONE);
                ivClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            } else {
                baseView.findViewById(R.id.tv_title).setVisibility(View.GONE);
            }
            if (contentView != null) {
                contView.removeAllViews();
                contView.addView(contentView);
            } else {
                tvContent.setText(strContent);
            }
            if (TextUtils.isEmpty(positiveText)) {  //确认
                tvPositive.setVisibility(View.GONE);
            } else {
                tvPositive.setText(positiveText);
                tvPositive.setTextColor(positiveColor);
                tvPositive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (positiveListener != null) {
                            positiveListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                        dialog.dismiss();
                    }
                });
            }
            if (TextUtils.isEmpty(negativeText)) {  //取消
                tvNegative.setVisibility(View.GONE);
            } else {
                tvNegative.setText(negativeText);
                tvNegative.setTextColor(negativeColor);
                tvNegative.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        if (negativeListener != null) {
                            negativeListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    }
                });
            }
            //设置外部点击不消失，防止事件未处理到
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
            dialog.addContentView(baseView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(baseView);
            return dialog;
        }
    }
}

